package cn.cscb.mms.server.service.impl;

import cn.cscb.mms.common.exception.AuthenticationExcetpion;
import cn.cscb.mms.pojo.dto.EmployeeLoginDTO;
import cn.cscb.mms.pojo.entity.Employee;
import cn.cscb.mms.pojo.vo.EmployeeLoginVO;
import cn.cscb.mms.server.mapper.EmployeeMapper;
import cn.cscb.mms.server.service.LoginService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service("loginService")
public class LoginServiceImpl implements LoginService {

    @Resource
    private EmployeeMapper employeeMapper;

    @Override
    public EmployeeLoginVO login(EmployeeLoginDTO dto) {
        QueryWrapper<Employee> wrapper = new QueryWrapper<>();
        wrapper.eq("emp_no", dto.getEmpNo());
        List<Employee> employees = employeeMapper.selectList(wrapper);
        if (employees.isEmpty()) {
            throw new AuthenticationExcetpion("用户名或密码错误");
        }
        Employee employee = employees.get(0);
        if (!dto.getPassword().equals(employee.getPassword())) {
            throw new AuthenticationExcetpion("用户名或密码错误");
        }
        EmployeeLoginVO vo = new EmployeeLoginVO();
        vo.setEmpNo(employee.getEmpNo());
        vo.setEmpName(employee.getEmpName());
        return vo;
    }
}
